home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 September / macformat-004.iso / Shareware City / Games / Jotto ][ 1.1.source Folder / Jotto ][ ƒ / Shell ƒ / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-06  |  13.1 KB  |  538 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        menus.c
  4.  
  5. Purpose:    This module handles menu selections.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "graphics.h"
  25. #include "menus.h"
  26. #include "help.h"
  27. #include "environment.h"
  28. #include "sounds.h"
  29. #include "error.h"
  30. #include "launch.h"
  31. #include "file interface.h"
  32. #include "jotto load-save.h"
  33. #include "jotto.h"
  34. #include "jotto environment.h"
  35. #include "debinhex dispatch.h"
  36. #include "file management.h"
  37. #include "program globals.h"
  38.  
  39. extern    long        menuDisable : 0x0b54;
  40.  
  41. Boolean            gMenuEnabled;
  42. MenuHandle        gAppleMenu;
  43. MenuHandle        gFileMenu;
  44. MenuHandle        gEditMenu;
  45. MenuHandle        gOptionsMenu;
  46. MenuHandle        gFluffMenu;
  47. MenuHandle        gGasketMenu;
  48.  
  49. enum
  50. {
  51.     appleMenu = 400, fileMenu, editMenu, optionsMenu, fluffMenu, gasketMenu,
  52.     
  53.     aboutItem = 1, aboutMSGItem, helpPointerItem,
  54.     
  55.     newItem = 1, openItem, closeItem, saveItem, saveAsItem, file_unused1, quitItem,
  56.     
  57.     undoItem = 1, edit_unused1, cutItem, copyItem, pasteItem, clearItem, edit_unused2,
  58.         selectAllItem,
  59.     
  60.     useFiveLetter = 1, useSixLetter, options_unused1, dupToggle, nonWordsToggle,
  61.         soundToggle, animationToggle, options_unused2, showNotePad, boardFrontItem,
  62.     
  63.     deBinHexItem = 1, launchItem, forceQuitItem, fluff_unused1, hideItem
  64. };
  65.  
  66. /*-----------------------------------------------------------------------------------*/
  67. /* internal stuff for menus.c                                                        */
  68.  
  69. void HandleAppleMenu(short menuItem);
  70. void HandleFileMenu(short menuItem);
  71. void HandleEditMenu(short menuItem);
  72. void HandleHelpMenu(void);
  73. void HandleOptionsMenu(short menuItem);
  74. void HandleFluffMenu(short menuItem);
  75.  
  76. Boolean InitTheMenus(void)
  77. {
  78.     Handle        MBARHandle;
  79.     
  80.     if ((MBARHandle=GetNewMBar(400))==0L)        /* sez which menus are in menu bar. */
  81.         return FALSE;
  82.     SetMenuBar(MBARHandle);                        /* set this to be THE menu bar to use. */
  83.     
  84.     if ((gAppleMenu=GetMHandle(appleMenu))==0L)    /* GetNewMBar also got menu handles of */
  85.         return FALSE;
  86.     if ((gFileMenu=GetMHandle(fileMenu))==0L)    /* every menu it includes, so just */
  87.         return FALSE;
  88.     if ((gEditMenu=GetMHandle(editMenu))==0L)    /* grab these handles and assign them */
  89.         return FALSE;
  90.     if ((gOptionsMenu=GetMHandle(optionsMenu))==0L)
  91.         return FALSE;
  92.     if ((gFluffMenu=GetMHandle(fluffMenu))==0L)
  93.         return FALSE;
  94.     if ((gGasketMenu=GetMHandle(gasketMenu))==0L)
  95.         return FALSE;
  96.     
  97.     if (gSystemSevenOrLater)
  98.     {
  99.         AppendMenu(gFluffMenu, "\p-");
  100.         AppendMenu(gFluffMenu, "\pHide/1");
  101.     }
  102.     
  103.     AddResMenu(gAppleMenu, 'DRVR');                /* adds control panels to apple menu */
  104.     
  105.     AdjustMenus();                                /* dim/enable/check/mark menus/items */
  106.     DrawMenuBar();                                /* draws the actual menu bar */
  107.     
  108.     return TRUE;
  109. }
  110.  
  111. void ShutDownTheMenus(void)
  112. {
  113.     ReleaseResource(gAppleMenu);
  114.     ReleaseResource(gFileMenu);
  115.     ReleaseResource(gEditMenu);
  116.     ReleaseResource(gOptionsMenu);
  117.     ReleaseResource(gFluffMenu);
  118.     ReleaseResource(gGasketMenu);
  119. }
  120.  
  121. void AdjustMenus(void)
  122. {
  123.     WindowPeek        theWindow;
  124.     short            kind;
  125.     
  126.     if (gInProgress)
  127.     {
  128.         DisableItem(gAppleMenu, aboutItem);
  129.         DisableItem(gAppleMenu, aboutMSGItem);
  130.         DisableItem(gAppleMenu, helpPointerItem);
  131.         DisableItem(gFileMenu, 0);
  132.         if (gSystemSevenOrLater)
  133.         {
  134.             DisableItem(gFluffMenu, deBinHexItem);
  135.             DisableItem(gFluffMenu, launchItem);
  136.             DisableItem(gFluffMenu, hideItem);
  137.         }
  138.         else DisableItem(gFluffMenu, 0);
  139.         
  140.         DisableItem(gEditMenu, 0);
  141.         DisableItem(gOptionsMenu, 0);
  142.         DisableItem(gGasketMenu, 0);
  143.     }
  144.     else
  145.     {
  146.         EnableItem(gAppleMenu, aboutItem);
  147.         EnableItem(gAppleMenu, aboutMSGItem);
  148.         EnableItem(gAppleMenu, helpPointerItem);
  149.         EnableItem(gFileMenu, 0);
  150.         if (gSystemSevenOrLater)
  151.         {
  152.             EnableItem(gFluffMenu, deBinHexItem);
  153.             EnableItem(gFluffMenu, launchItem);
  154.             EnableItem(gFluffMenu, hideItem);
  155.         }
  156.         else EnableItem(gFluffMenu, 0);
  157.         
  158.         EnableItem(gEditMenu, 0);
  159.         EnableItem(gOptionsMenu, 0);
  160.         EnableItem(gGasketMenu, 0);
  161.         
  162.         theWindow = (WindowPeek)FrontWindow();
  163.         kind = theWindow ? theWindow->windowKind : 0;
  164.         
  165.         if ((kind < 0) ||
  166.             ((gTheWindow[kNotePad]!=0L) && (gTheWindow[kNotePad]==FrontWindow())))
  167.         {
  168.             if (kind<0)
  169.                 EnableItem(gEditMenu, undoItem);
  170.             else
  171.                 DisableItem(gEditMenu, undoItem);
  172.             EnableItem(gEditMenu, cutItem);
  173.             EnableItem(gEditMenu, copyItem);
  174.             EnableItem(gEditMenu, pasteItem);
  175.             EnableItem(gEditMenu, clearItem);
  176.             EnableItem(gEditMenu, selectAllItem);
  177.         }
  178.         else
  179.         {
  180.             DisableItem(gEditMenu, undoItem);
  181.             DisableItem(gEditMenu, cutItem);
  182.             DisableItem(gEditMenu, copyItem);
  183.             DisableItem(gEditMenu, pasteItem);
  184.             DisableItem(gEditMenu, clearItem);
  185.             DisableItem(gEditMenu, selectAllItem);
  186.         }
  187.         
  188.         if(theWindow)
  189.             EnableItem(gFileMenu, closeItem);
  190.         else
  191.             DisableItem(gFileMenu, closeItem);
  192.         
  193.         if (FrontWindow()!=0L)
  194.         {
  195.             if ((gTheWindow[kMainWindow]!=0L) && (gTheWindow[kMainWindow]!=FrontWindow()))
  196.             {
  197.                 SetItem(gOptionsMenu, boardFrontItem, "\pBring board to front");
  198.                 EnableItem(gOptionsMenu, boardFrontItem);
  199.             }
  200.             else if (gTheWindow[kNotePad]!=0L)
  201.             {
  202.                 SetItem(gOptionsMenu, boardFrontItem, "\pBring note pad to front");
  203.                 if (gTheWindow[kNotePad]==FrontWindow())
  204.                     DisableItem(gOptionsMenu, boardFrontItem);
  205.                 else
  206.                     EnableItem(gOptionsMenu, boardFrontItem);
  207.             }
  208.             else
  209.             {
  210.                 SetItem(gOptionsMenu, boardFrontItem, "\pBring board to front");
  211.                 DisableItem(gOptionsMenu, boardFrontItem);
  212.             }
  213.         }
  214.         else
  215.         {
  216.             SetItem(gOptionsMenu, boardFrontItem, "\pBring board to front");
  217.             DisableItem(gOptionsMenu, boardFrontItem);
  218.         }
  219.         
  220.         if (gTheWindow[kMainWindow])
  221.         {
  222.             EnableItem(gFileMenu, saveItem);
  223.             EnableItem(gFileMenu, saveAsItem);
  224.             DisableItem(gOptionsMenu, dupToggle);
  225.             DisableItem(gOptionsMenu, useFiveLetter);
  226.             DisableItem(gOptionsMenu, useSixLetter);
  227.         }
  228.         else
  229.         {
  230.             DisableItem(gFileMenu, saveItem);
  231.             DisableItem(gFileMenu, saveAsItem);
  232.             EnableItem(gOptionsMenu, dupToggle);
  233.             if (FiveLetterOKQQ())
  234.                 EnableItem(gOptionsMenu, useFiveLetter);
  235.             else
  236.                 DisableItem(gOptionsMenu, useFiveLetter);
  237.             
  238.             if (SixLetterOKQQ())
  239.                 EnableItem(gOptionsMenu, useSixLetter);
  240.             else
  241.                 DisableItem(gOptionsMenu, useSixLetter);
  242.         }
  243.         
  244.         if (gSystemSevenOrLater)
  245.             EnableItem(gFluffMenu, forceQuitItem);
  246.         else
  247.             DisableItem(gFluffMenu, forceQuitItem);
  248.         
  249.         if (gSoundAvailable)
  250.             EnableItem(gOptionsMenu, soundToggle);
  251.         else
  252.             DisableItem(gOptionsMenu, soundToggle);
  253.     }
  254.     CheckItem(gOptionsMenu, dupToggle, gAllowDup);
  255.     CheckItem(gOptionsMenu, nonWordsToggle, gNonWordsCount);
  256.     CheckItem(gOptionsMenu, animationToggle, gAnimation);
  257.     SetItemMark(gOptionsMenu, useFiveLetter, (gNumLetters==5) ? '◊' : noMark);
  258.     SetItemMark(gOptionsMenu, useSixLetter, (gNumLetters==6) ? '◊' : noMark);
  259.     CheckItem(gOptionsMenu, showNotePad, (gTheWindow[kNotePad]!=0L));
  260.     CheckItem(gOptionsMenu, soundToggle, gSoundToggle&&gSoundAvailable);
  261. }
  262.  
  263. void HandleMenu(long mSelect)
  264. {
  265.     short            menuID = HiWord(mSelect);
  266.     short            menuItem = LoWord(mSelect);
  267.     
  268.     if (menuID==0)
  269.     {
  270.         menuID=HiWord(menuDisable);
  271.         menuItem=LoWord(menuDisable);
  272.         gMenuEnabled=FALSE;
  273.     }
  274.     else gMenuEnabled=TRUE;
  275.     menuDisable=0L;
  276.  
  277.     switch (menuID)
  278.     {
  279.         case appleMenu:
  280.             HandleAppleMenu(menuItem);
  281.             break;
  282.         case fileMenu:
  283.             HandleFileMenu(menuItem);
  284.             break;    
  285.         case editMenu:
  286.             HandleEditMenu(menuItem);
  287.             break;
  288.         case optionsMenu:
  289.             HandleOptionsMenu(menuItem);
  290.             break;
  291.         case fluffMenu:
  292.             HandleFluffMenu(menuItem);
  293.             break;
  294.     }
  295. }
  296.  
  297. void DoTheCloseThing(WindowPeek theWindow)
  298. /* a standard close procedure, called when "close" is chosen from File menu and when
  299.    a window is closed through its close box */
  300. {
  301.     Boolean            gotone;
  302.     short            i;
  303.     short            kind;
  304.     
  305.     if (theWindow==0L)
  306.         return;
  307.     
  308.     kind = theWindow ? theWindow->windowKind : 0;
  309.     if (kind<0)        /* DA window or other system window */
  310.         CloseDeskAcc(kind);
  311.     else
  312.     {
  313.         gotone=FALSE;
  314.         /* see if it's one of ours */
  315.         for (i=0; (i<NUM_WINDOWS) && (!gotone); i++)
  316.             gotone=((WindowPtr)theWindow==gTheWindow[i]);
  317.         
  318.         if (gotone)        /* if it's one of ours...  see graphics.c */
  319.             CloseTheWindow(gTheWindowData[i-1]);    /* this may return FALSE = not closed */
  320.         else
  321.             DisposeWindow((WindowPtr)theWindow);        /* not one of ours, so just close it */
  322.     
  323.         AdjustMenus();    /* may affect which menu items or menus are available, etc */
  324.     }
  325. }
  326.  
  327. void HandleAppleMenu(short menuItem)
  328. {
  329.     GrafPtr            savePort;
  330.     Str255            name;
  331.     
  332.     switch (menuItem)
  333.     {
  334.         case aboutItem:
  335.             if (gMenuEnabled)
  336.             {
  337.                 OpenTheWindow(kAbout);
  338.             }
  339.             else DoSound(sound_fluff, TRUE);
  340.             break;
  341.         case aboutMSGItem:
  342.             if (gMenuEnabled)
  343.                 OpenTheWindow(kAboutMSG);
  344.             else DoSound(sound_fluff, TRUE);
  345.             break;
  346.         case helpPointerItem:
  347.             if (gMenuEnabled)
  348.                 HandleHelpMenu();
  349.             else DoSound(sound_fluff, TRUE);
  350.             break;
  351.         default:
  352.             if (menuItem > helpPointerItem+1)
  353.             {
  354.                 GetPort(&savePort);
  355.                 GetItem(gAppleMenu, menuItem, name);
  356.                 OpenDeskAcc(name);
  357.                 SetPort(savePort);
  358.             }
  359.             break;
  360.     }
  361. }
  362.  
  363. void HandleFileMenu(short menuItem)
  364. {
  365.     WindowPtr        theWindow;
  366.     short            i;
  367.     Boolean            gotone;
  368.     
  369.     switch (menuItem)
  370.     {
  371.         case newItem:
  372.             if (gMenuEnabled)
  373.                 NewGame();
  374.             else DoSound(sound_fluff, TRUE);
  375.             break;
  376.         case openItem:
  377.             if (gMenuEnabled)
  378.                 LoadSaveDispatch(TRUE, FALSE);
  379.             else DoSound(sound_fluff, TRUE);
  380.             break;
  381.         case closeItem:
  382.             if (gMenuEnabled)
  383.                 DoTheCloseThing((WindowPeek)FrontWindow());
  384.             else DoSound(sound_fluff, TRUE);
  385.             break;
  386.         case saveItem:
  387.             if (gMenuEnabled)
  388.                 LoadSaveDispatch(FALSE, TRUE);
  389.             else DoSound(sound_fluff, TRUE);
  390.             break;
  391.         case saveAsItem:
  392.             if (gMenuEnabled)
  393.                 LoadSaveDispatch(FALSE, FALSE);
  394.             else DoSound(sound_fluff, TRUE);
  395.             break;
  396.         case quitItem:
  397.             if (gMenuEnabled)
  398.                 gDone = TRUE;
  399.             else DoSound(sound_fluff, TRUE);
  400.             break;
  401.     }
  402. }
  403.  
  404. void HandleEditMenu(short menuItem)
  405. {
  406.     ExtendedWindowDataHandle    theData;
  407.     
  408.     if ((menuItem==0) || (menuItem==2) || (menuItem>CountMItems(gEditMenu)))
  409.         return;
  410.     
  411.     if (gMenuEnabled)
  412.     {
  413.         if (gFrontWindowIsOurs)
  414.         {
  415.             theData=(ExtendedWindowDataHandle)GetWRefCon(FrontWindow());
  416.             switch (menuItem)
  417.             {
  418.                 case undoItem:
  419.                     ((**theData).dispatchProc)((WindowDataHandle)theData, kUndo, 0L);
  420.                     break;
  421.                 case cutItem:
  422.                     ((**theData).dispatchProc)((WindowDataHandle)theData, kCut, 0L);
  423.                     break;
  424.                 case copyItem:
  425.                     ((**theData).dispatchProc)((WindowDataHandle)theData, kCopy, 0L);
  426.                     break;
  427.                 case pasteItem:
  428.                     ((**theData).dispatchProc)((WindowDataHandle)theData, kPaste, 0L);
  429.                     break;
  430.                 case clearItem:
  431.                     ((**theData).dispatchProc)((WindowDataHandle)theData, kClear, 0L);
  432.                     break;
  433.                 case selectAllItem:
  434.                     ((**theData).dispatchProc)((WindowDataHandle)theData, kSelectAll, 0L);
  435.                     break;
  436.             }
  437.         }
  438.         else SystemEdit(menuItem - 1);
  439.     }
  440.     else DoSound(sound_fluff, TRUE);
  441. }
  442.  
  443. void HandleOptionsMenu(short menuItem)
  444. {
  445.     WindowPtr            theWindow;
  446.     
  447.     switch (menuItem)
  448.     {
  449.         case dupToggle:
  450.         case nonWordsToggle:
  451.         case useFiveLetter:
  452.         case useSixLetter:
  453.         case animationToggle:
  454.         case soundToggle:
  455.             if (gMenuEnabled)
  456.             {
  457.                 switch (menuItem)
  458.                 {
  459.                     case dupToggle:            gAllowDup=!gAllowDup; break;
  460.                     case nonWordsToggle:    gNonWordsCount=!gNonWordsCount; break;
  461.                     case useFiveLetter:        gNumLetters=0x05; break;
  462.                     case useSixLetter:        gNumLetters=0x06; break;
  463.                     case animationToggle:    gAnimation=!gAnimation; break;
  464.                     case soundToggle:        gSoundToggle=!gSoundToggle; DoSound(sound_on, TRUE); break;
  465.                 }
  466.             }
  467.             else DoSound(sound_fluff, TRUE);
  468.             break;
  469.         case showNotePad:
  470.             if (gMenuEnabled)
  471.             {
  472.                 if (gTheWindow[kNotePad])
  473.                 {
  474.                     SelectWindow(gTheWindow[kNotePad]);
  475.                     CloseTheWindow(gTheWindowData[kNotePad]);
  476.                 }
  477.                 else
  478.                 {
  479.                     OpenTheWindow(kNotePad);
  480.                 }
  481.             }
  482.             else DoSound(sound_fluff, TRUE);
  483.             break;
  484.         case boardFrontItem:
  485.             if (FrontWindow()!=0L)
  486.             {
  487.                 if ((gTheWindow[kMainWindow]!=0L) && (gTheWindow[kMainWindow]!=FrontWindow()))
  488.                 {
  489.                     SelectWindow(gTheWindow[kMainWindow]);
  490.                 }
  491.                 else if (gTheWindow[kNotePad]!=0L)
  492.                 {
  493.                     SelectWindow(gTheWindow[kNotePad]);
  494.                 }
  495.                 else DoSound(sound_fluff, TRUE);
  496.             }
  497.             else DoSound(sound_fluff, TRUE);
  498.     }
  499. }
  500.  
  501. void HandleHelpMenu(void)
  502. {
  503.     OpenTheWindow(kHelp);
  504. }
  505.  
  506. void HandleFluffMenu(short menuItem)
  507. {
  508.     switch (menuItem)
  509.     {
  510.         case deBinHexItem:
  511.             if (gMenuEnabled)
  512.             {
  513.                 if (GetSourceFile(&inputFS, TRUE, FALSE))
  514.                     HandleError(DeBinHexDispatch(), FALSE);
  515.             }
  516.             else DoSound(sound_fluff, TRUE);
  517.             break;
  518.         case launchItem:
  519.             if (gMenuEnabled)
  520.             {
  521.                 LaunchDispatch();
  522.             }
  523.             else DoSound(sound_fluff, TRUE);
  524.         case forceQuitItem:
  525.             if (gMenuEnabled)
  526.                 SysError(0x4e22);
  527.             else DoSound(sound_fluff, TRUE);
  528.             break;
  529.         case hideItem:
  530.             if (gMenuEnabled)
  531.             {
  532.                 MenuKey(0);
  533.             }
  534.             else DoSound(sound_fluff, TRUE);
  535.             break;
  536.     }
  537. }
  538.